#include "DepthBuffer.h"

namespace egg{

    DepthBuffer::DepthBuffer(uint32 w,uint32 h,bool stencil){
        mStencil=stencil;
        mWidth=w,mHeight=h;
        if(stencil)
            mFormat=GL_DEPTH24_STENCIL8;
        else
            mFormat=GL_DEPTH_COMPONENT32;
        glGenRenderbuffers(1,&mID);
        glBindRenderbuffer(GL_RENDERBUFFER,mID);
        glRenderbufferStorage(GL_RENDERBUFFER,mFormat,w,h);
        glBindRenderbuffer(GL_RENDERBUFFER,0);
        RHILogError("DepthBuffer::construuct");
    }

    DepthBuffer::~DepthBuffer(){
        glDeleteRenderbuffers(1,&mID);
        RHILogError("DepthBuffer::destruct");
    }

    void DepthBuffer::resize(uint32 w,uint32 h){
        if(mWidth==w&&mHeight==h)
            return;
        mWidth=w,mHeight=h;
        glBindRenderbuffer(GL_RENDERBUFFER,mID);
        glRenderbufferStorage(GL_RENDERBUFFER,mFormat,w,h);
        glBindRenderbuffer(GL_RENDERBUFFER,0);
        RHILogError("DepthBuffer::resize");
    }

}